package com.xkdx.caipiao.wiget;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.hall.CaiPiaoActivity;
import com.xkdx.caipiao.listener.OnBallClickListener;
@SuppressLint("ResourceAsColor")
public class WhiteBallButton extends Button implements OnTouchListener {
	Context context;
	String text;
	boolean isSelected = false;
	String mark = "";
	OnBallClickListener listener;
	View self;

	public WhiteBallButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	public WhiteBallButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public WhiteBallButton(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getMark() {
		return mark;
	}

	public boolean getState() {
		return isSelected;
	}

	public void setLottoState(ArrayList<String> set) {

		if (set.contains(text)) {
			isSelected = true;
			setBackgroundResource(R.drawable.red_ball_bg_caipiao);
			setTextColor(getResources().getColor(R.color.white_caipiao));
			
		} else {
			isSelected = false;
			setBackgroundResource(R.drawable.ball_gray_caipiao);
			setTextColor(getResources().getColor(R.color.red_caipiao));
		}
		// invalidate();
	}

	public void setState(String num) {
		if (num.equals(text)) {
			isSelected = true;
			setBackgroundResource(R.drawable.red_ball_bg_caipiao);
			setTextColor(getResources().getColor(R.color.white_caipiao));
		} else {
			isSelected = false;
			setBackgroundResource(R.drawable.ball_gray_caipiao);
			setTextColor(getResources().getColor(R.color.red_caipiao));
		}
		// invalidate();
	}

	public void setSelected() {
		isSelected = true;
		setBackgroundResource(R.drawable.red_ball_bg_caipiao);
		setTextColor(getResources().getColor(R.color.white_caipiao));
		// invalidate();
	}

	@SuppressLint("ResourceAsColor")
	private void init() {
		self = this;
		setOnTouchListener(this);
		setBackgroundResource(R.drawable.ball_gray_caipiao);
		setTextColor(getResources().getColor(R.color.red_caipiao));
		this.post(new Runnable() {

			@Override
			public void run() {
				text = getText().toString();
			}
		});

	}

	public void setBallClickListener(OnBallClickListener l) {
		this.listener = l;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch(event.getAction()){
		case MotionEvent.ACTION_DOWN:
//			listener.ShowBallPopoWidow(self, text);
			break;
		case MotionEvent.ACTION_MOVE:
			listener.DismissBallPopowindow();
			break;
		case MotionEvent.ACTION_UP:
			if (isSelected) {
				isSelected = !isSelected;
				setBackgroundResource(R.drawable.ball_gray_caipiao);
				setTextColor(getResources().getColor(R.color.red_caipiao));
				listener.RemoveBall(mark, text);

			} else {
				isSelected = !isSelected;
				setBackgroundResource(R.drawable.red_ball_bg_caipiao);
				setTextColor(getResources().getColor(R.color.white_caipiao));
				listener.AddBall(mark, text);

			}
			listener.DismissBallPopowindow();

			((CaiPiaoActivity) context).setMutiplyList();
			break;
		}
		return true;
	}



}
